using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;

namespace Infralution.Localization.Wpf
{
	public abstract class ManagedMarkupExtension1 : MarkupExtension
	{
		private List<WeakReference> list_0 = new List<WeakReference>();

		private object object_0;

		public bool IsTargetAlive
		{
			get
			{
				if (list_0.Count == 0)
				{
					return true;
				}
				foreach (WeakReference item in list_0)
				{
					if (item.IsAlive)
					{
						return true;
					}
				}
				return false;
			}
		}

		public bool IsInDesignMode
		{
			get
			{
				foreach (WeakReference item in list_0)
				{
					DependencyObject dependencyObject = item.Target as DependencyObject;
					if (dependencyObject != null && DesignerProperties.GetIsInDesignMode(dependencyObject))
					{
						return true;
					}
				}
				return false;
			}
		}

		protected List<WeakReference> TargetObjects => list_0;

		protected object TargetProperty => object_0;

		protected Type TargetPropertyType
		{
			get
			{
				Type result = null;
				if (object_0 is DependencyProperty)
				{
					result = (object_0 as DependencyProperty).PropertyType;
				}
				else if (object_0 is PropertyInfo)
				{
					result = (object_0 as PropertyInfo).PropertyType;
				}
				else if (object_0 != null)
				{
					result = object_0.GetType();
				}
				return result;
			}
		}

		public ManagedMarkupExtension1(MarkupExtensionManager1 manager)
		{
			manager.method_0(this);
		}

		public override object ProvideValue(IServiceProvider serviceProvider)
		{
			RegisterTarget(serviceProvider);
			object result = this;
			if (object_0 != null)
			{
				result = GetValue();
			}
			return result;
		}

		protected virtual void RegisterTarget(IServiceProvider serviceProvider)
		{
			IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
			object targetObject = provideValueTarget.TargetObject;
			if (targetObject != null && targetObject.GetType().FullName != "System.Windows.SharedDp")
			{
				object_0 = provideValueTarget.TargetProperty;
				list_0.Add(new WeakReference(targetObject));
			}
		}

		protected virtual void UpdateTarget(object target)
		{
			if (object_0 is DependencyProperty)
			{
				(target as DependencyObject)?.SetValue(object_0 as DependencyProperty, GetValue());
			}
			else if (object_0 is PropertyInfo)
			{
				(object_0 as PropertyInfo).SetValue(target, GetValue(), null);
			}
		}

		public void UpdateTargets()
		{
			foreach (WeakReference item in list_0)
			{
				if (item.IsAlive)
				{
					UpdateTarget(item.Target);
				}
			}
		}

		public bool IsTarget(object target)
		{
			foreach (WeakReference item in list_0)
			{
				if (item.IsAlive && item.Target == target)
				{
					return true;
				}
			}
			return false;
		}

		protected abstract object GetValue();
	}
}
